//<p>计算给定二叉树的所有左叶子之和。</p>
//
//<p><strong>示例：</strong></p>
//
//<pre>
//    3
//   / \
//  9  20
//    /  \
//   15   7
//
//在这个二叉树中，有两个左叶子，分别是 9 和 15，所以返回 24</pre>
//
//<p>&nbsp;</p>
//<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>广度优先搜索</li><li>二叉树</li></div></div><br><div><li>👍 388</li><li>👎 0</li></div>
  
package leetcode.editor.cn;

public class _404_SumOfLeftLeaves{
    public static void main(String[] args) {
         Solution solution = new _404_SumOfLeftLeaves().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int sumOfLeftLeaves1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = sumOfLeftLeaves1(root.left);
        int right = sumOfLeftLeaves1(root.right);
        int sum = 0;
        if (root.left!=null && root.left.right == null && root.left.left == null) {
            sum = root.left.val;
        }
        sum = sum+left+right;
        return sum;
    }



    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = sumOfLeftLeaves(root.left);
        int right = sumOfLeftLeaves(root.right);
        int sum = 0;
        if (root.left!=null && root.left.left==null && root.left.right==null) {
            sum = root.left.val;
        }
        sum = sum+left+right;
        return sum;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}